/**
 */
package BlipLanguage.impl;

import BlipLanguage.AndroidTransformer;
import BlipLanguage.BlipApplication;
import BlipLanguage.BlipLanguagePackage;

import BlipLanguage.BlipModel;
import BlipLanguage.BlipModelAttribute;
import MocaTree.Attribute;
import MocaTree.File;
import MocaTree.Folder;
import MocaTree.MocaTreeFactory;

import MocaTree.Node;
import de.upb.tools.sdm.*;

import java.lang.reflect.InvocationTargetException;

import java.util.*;
import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.impl.EObjectImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Android Transformer</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class AndroidTransformerImpl extends EObjectImpl implements
		AndroidTransformer {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected AndroidTransformerImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return BlipLanguagePackage.Literals.ANDROID_TRANSFORMER;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Folder transformApplication(BlipApplication application) {
		boolean fujaba__Success = false;
		Folder rootFolder = null;
		Folder resFolder = null;
		Folder srcFolder = null;
		Folder srcDeFolder = null;
		Folder resValuesFolder = null;
		Folder srcDeTudFolder = null;
		Folder srcDeTudEtitFolder = null;
		Folder srcApplicationFolder = null;
		Folder entityFolder = null;
		File file = null;
		Node rootNode = null;
		Attribute libraryNode = null;
		Attribute targetNode = null;
		Node rootContentNode = null;
		Attribute mapsApiKeyNode = null;
		Attribute packageNameNode = null;
		Attribute appNameAttr = null;
		Node packageNode = null;
		Node entityNode = null;
		Node importsNode = null;
		Attribute packageAttr = null;
		Node importsSerialNode = null;
		Node importsDateNode = null;
		Attribute classNameAttr = null;
		Node attributeNode = null;
		Attribute attributeTypeAttr = null;
		Attribute attributeNameAttr = null;
		Attribute attributeUpperNameAttr = null;
		Iterator fujaba__IterModelToAttribute = null;
		BlipModelAttribute attribute = null;
		Iterator fujaba__IterApplicationToModel = null;
		BlipModel model = null;

		// story node 'initFolder'
		try {
			fujaba__Success = false;

			// check object application is really bound
			JavaSDM.ensure(application != null);
			// create object rootFolder
			rootFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// create object resFolder
			resFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// create object srcFolder
			srcFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// create object srcDeFolder
			srcDeFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// create object resValuesFolder
			resValuesFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// create object srcDeTudFolder
			srcDeTudFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// create object srcDeTudEtitFolder
			srcDeTudEtitFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// create object srcApplicationFolder
			srcApplicationFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// create object entityFolder
			entityFolder = MocaTreeFactory.eINSTANCE.createFolder();

			// assign attribute rootFolder
			rootFolder.setName("android");
			// assign attribute srcFolder
			srcFolder.setName("src");
			// assign attribute resFolder
			resFolder.setName("res");
			// assign attribute resValuesFolder
			resValuesFolder.setName("values");
			// assign attribute srcDeFolder
			srcDeFolder.setName("de");
			// assign attribute srcDeTudFolder
			srcDeTudFolder.setName("tud");
			// assign attribute srcDeTudEtitFolder
			srcDeTudEtitFolder.setName("etit");
			// assign attribute srcApplicationFolder
			srcApplicationFolder.setName("blipz");
			// assign attribute entityFolder
			entityFolder.setName("entities");

			// create link
			resFolder.setParentFolder(rootFolder);

			// create link
			srcFolder.setParentFolder(rootFolder);

			// create link
			srcDeFolder.setParentFolder(srcFolder);

			// create link
			resValuesFolder.setParentFolder(resFolder);

			// create link
			srcDeTudFolder.setParentFolder(srcDeFolder);

			// create link
			srcDeTudEtitFolder.setParentFolder(srcDeTudFolder);

			// create link
			srcApplicationFolder.setParentFolder(srcDeTudEtitFolder);

			// create link
			entityFolder.setParentFolder(srcApplicationFolder);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story node 'createProjectProperties'
		try {
			fujaba__Success = false;

			// check object application is really bound
			JavaSDM.ensure(application != null);
			// check object rootFolder is really bound
			JavaSDM.ensure(rootFolder != null);
			// create object file
			file = MocaTreeFactory.eINSTANCE.createFile();

			// create object rootNode
			rootNode = MocaTreeFactory.eINSTANCE.createNode();

			// create object libraryNode
			libraryNode = MocaTreeFactory.eINSTANCE.createAttribute();

			// create object targetNode
			targetNode = MocaTreeFactory.eINSTANCE.createAttribute();

			// assign attribute file
			file.setName("project.properties");
			// assign attribute rootNode
			rootNode.setName("Properties");
			// assign attribute targetNode
			targetNode.setName("target");
			// assign attribute targetNode
			targetNode.setValue("7");
			// assign attribute libraryNode
			libraryNode.setName("android.library.reference.1");
			// assign attribute libraryNode
			libraryNode.setValue(application.getAndroidPlayLibraryPath());

			// create link
			file.setFolder(rootFolder);

			// create link
			rootNode.setFile(file);

			// create link
			libraryNode.setNode(rootNode);

			// create link
			targetNode.setNode(rootNode);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story node 'createAndroidManifest'
		try {
			fujaba__Success = false;

			// check object application is really bound
			JavaSDM.ensure(application != null);
			// check object rootFolder is really bound
			JavaSDM.ensure(rootFolder != null);
			// create object file
			file = MocaTreeFactory.eINSTANCE.createFile();

			// create object rootNode
			rootNode = MocaTreeFactory.eINSTANCE.createNode();

			// create object rootContentNode
			rootContentNode = MocaTreeFactory.eINSTANCE.createNode();

			// create object mapsApiKeyNode
			mapsApiKeyNode = MocaTreeFactory.eINSTANCE.createAttribute();

			// create object packageNameNode
			packageNameNode = MocaTreeFactory.eINSTANCE.createAttribute();

			// assign attribute file
			file.setName("AndroidManifest.xml");
			// assign attribute rootNode
			rootNode.setName("XML");
			// assign attribute rootContentNode
			rootContentNode.setName("AndroidManifest");
			// assign attribute packageNameNode
			packageNameNode.setName("packageName");
			// assign attribute packageNameNode
			packageNameNode.setValue(application.getPackageName());
			// assign attribute packageNameNode
			packageNameNode.setIndex(0);
			// assign attribute mapsApiKeyNode
			mapsApiKeyNode.setName("mapsApiKey");
			// assign attribute mapsApiKeyNode
			mapsApiKeyNode.setValue(application.getAndroidMapsApiKey());
			// assign attribute mapsApiKeyNode
			mapsApiKeyNode.setIndex(1);

			// create link
			file.setFolder(rootFolder);

			// create link
			rootNode.setFile(file);

			// create link
			rootContentNode.setParentNode(rootNode);

			// create link
			mapsApiKeyNode.setNode(rootContentNode);

			// create link
			packageNameNode.setNode(rootContentNode);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story node 'createStrings'
		try {
			fujaba__Success = false;

			// check object application is really bound
			JavaSDM.ensure(application != null);
			// check object resValuesFolder is really bound
			JavaSDM.ensure(resValuesFolder != null);
			// create object file
			file = MocaTreeFactory.eINSTANCE.createFile();

			// create object rootNode
			rootNode = MocaTreeFactory.eINSTANCE.createNode();

			// create object rootContentNode
			rootContentNode = MocaTreeFactory.eINSTANCE.createNode();

			// create object appNameAttr
			appNameAttr = MocaTreeFactory.eINSTANCE.createAttribute();

			// assign attribute file
			file.setName("strings.xml");
			// assign attribute rootNode
			rootNode.setName("XML");
			// assign attribute rootContentNode
			rootContentNode.setName("Strings");
			// assign attribute appNameAttr
			appNameAttr.setName("app_name");
			// assign attribute appNameAttr
			appNameAttr.setValue(application.getTitle());

			// create link
			file.setFolder(resValuesFolder);

			// create link
			rootNode.setFile(file);

			// create link
			rootContentNode.setParentNode(rootNode);

			// create link
			appNameAttr.setNode(rootContentNode);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story node 'modelInApplication'
		try {
			fujaba__Success = false;

			// check object application is really bound
			JavaSDM.ensure(application != null);
			// iterate to-many link models from application to model
			fujaba__Success = false;

			fujaba__IterApplicationToModel = new ArrayList(
					application.getModels()).iterator();

			while (fujaba__IterApplicationToModel.hasNext()) {
				try {
					model = (BlipModel) fujaba__IterApplicationToModel.next();

					// check object model is really bound
					JavaSDM.ensure(model != null);
					// story node 'createEntity'
					try {
						fujaba__Success = false;

						// check object application is really bound
						JavaSDM.ensure(application != null);
						// check object entityFolder is really bound
						JavaSDM.ensure(entityFolder != null);
						// check object model is really bound
						JavaSDM.ensure(model != null);
						// create object file
						file = MocaTreeFactory.eINSTANCE.createFile();

						// create object rootNode
						rootNode = MocaTreeFactory.eINSTANCE.createNode();

						// create object packageNode
						packageNode = MocaTreeFactory.eINSTANCE.createNode();

						// create object entityNode
						entityNode = MocaTreeFactory.eINSTANCE.createNode();

						// create object importsNode
						importsNode = MocaTreeFactory.eINSTANCE.createNode();

						// create object packageAttr
						packageAttr = MocaTreeFactory.eINSTANCE
								.createAttribute();

						// create object importsSerialNode
						importsSerialNode = MocaTreeFactory.eINSTANCE
								.createNode();

						// create object importsDateNode
						importsDateNode = MocaTreeFactory.eINSTANCE
								.createNode();

						// create object classNameAttr
						classNameAttr = MocaTreeFactory.eINSTANCE
								.createAttribute();

						// assign attribute file
						file.setName(model.getName() + ".java");
						// assign attribute rootNode
						rootNode.setName("JAVA");
						// assign attribute packageNode
						packageNode.setIndex(0);
						// assign attribute packageNode
						packageNode.setName("Package");
						// assign attribute packageAttr
						packageAttr.setName("name");
						// assign attribute packageAttr
						packageAttr.setValue(application.getPackageName()
								+ ".entities");
						// assign attribute packageAttr
						packageAttr.setIndex(0);
						// assign attribute importsNode
						importsNode.setIndex(1);
						// assign attribute importsNode
						importsNode.setName("Imports");
						// assign attribute importsSerialNode
						importsSerialNode.setName("java.io.Serializable");
						// assign attribute importsDateNode
						importsDateNode.setName("java.util.Date");
						// assign attribute entityNode
						entityNode.setIndex(2);
						// assign attribute entityNode
						entityNode.setName("Entity");
						// assign attribute classNameAttr
						classNameAttr.setName("className");
						// assign attribute classNameAttr
						classNameAttr.setValue(model.getName());
						// assign attribute classNameAttr
						classNameAttr.setIndex(0);

						// create link
						file.setFolder(entityFolder);

						// create link
						rootNode.setFile(file);

						// create link
						packageNode.setParentNode(rootNode);

						// create link
						entityNode.setParentNode(rootNode);

						// create link
						importsNode.setParentNode(rootNode);

						// create link
						packageAttr.setNode(packageNode);

						// create link
						importsSerialNode.setParentNode(importsNode);

						// create link
						importsDateNode.setParentNode(importsNode);

						// create link
						classNameAttr.setNode(entityNode);

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					// story node 'attributeInModel'
					try {
						fujaba__Success = false;

						// check object model is really bound
						JavaSDM.ensure(model != null);
						// iterate to-many link attributes from model to attribute
						fujaba__Success = false;

						fujaba__IterModelToAttribute = new ArrayList(
								model.getAttributes()).iterator();

						while (fujaba__IterModelToAttribute.hasNext()) {
							try {
								attribute = (BlipModelAttribute) fujaba__IterModelToAttribute
										.next();

								// check object attribute is really bound
								JavaSDM.ensure(attribute != null);
								// story node 'createAttributeNode'
								try {
									fujaba__Success = false;

									// check object attribute is really bound
									JavaSDM.ensure(attribute != null);
									// check object entityNode is really bound
									JavaSDM.ensure(entityNode != null);
									// create object attributeNode
									attributeNode = MocaTreeFactory.eINSTANCE
											.createNode();

									// create object attributeTypeAttr
									attributeTypeAttr = MocaTreeFactory.eINSTANCE
											.createAttribute();

									// create object attributeNameAttr
									attributeNameAttr = MocaTreeFactory.eINSTANCE
											.createAttribute();

									// create object attributeUpperNameAttr
									attributeUpperNameAttr = MocaTreeFactory.eINSTANCE
											.createAttribute();

									// assign attribute attributeNode
									attributeNode.setName("Attribute");
									// assign attribute attributeNameAttr
									attributeNameAttr.setName("name");
									// assign attribute attributeNameAttr
									attributeNameAttr.setValue(attribute
											.getName());
									// assign attribute attributeNameAttr
									attributeNameAttr.setIndex(0);
									// assign attribute attributeUpperNameAttr
									attributeUpperNameAttr
											.setName("firstUpperName");
									// assign attribute attributeUpperNameAttr
									attributeUpperNameAttr.setValue(attribute
											.getName().substring(0, 1)
											.toUpperCase()
											+ attribute.getName().substring(1));
									// assign attribute attributeUpperNameAttr
									attributeUpperNameAttr.setIndex(1);
									// assign attribute attributeTypeAttr
									attributeTypeAttr.setName("type");
									// assign attribute attributeTypeAttr
									attributeTypeAttr.setValue(attribute
											.getType());
									// assign attribute attributeTypeAttr
									attributeTypeAttr.setIndex(2);

									// create link
									attributeNode.setParentNode(entityNode);

									// create link
									attributeTypeAttr.setNode(attributeNode);

									// create link
									attributeNameAttr.setNode(attributeNode);

									// create link
									attributeUpperNameAttr
											.setNode(attributeNode);

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}

								fujaba__Success = true;
							} catch (JavaSDMException fujaba__InternalException) {
								fujaba__Success = false;
							}
						}
						JavaSDM.ensure(fujaba__Success);
						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return rootFolder;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public File transformModelToEntity(BlipModel model, Folder entityFolder) {
		boolean fujaba__Success = false;
		File entityFile = null;
		Node entityRootNode = null;

		// story node 'initEntityFile'
		try {
			fujaba__Success = false;

			// create object entityFile
			entityFile = MocaTreeFactory.eINSTANCE.createFile();

			// create object entityRootNode
			entityRootNode = MocaTreeFactory.eINSTANCE.createNode();

			// assign attribute entityFile
			entityFile.setName(model.getName() + ".java");
			// assign attribute entityRootNode
			entityRootNode.setName("Entity");

			// create link
			entityRootNode.setFile(entityFile);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return entityFile;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments)
			throws InvocationTargetException {
		switch (operationID) {
		case BlipLanguagePackage.ANDROID_TRANSFORMER___TRANSFORM_APPLICATION__BLIPAPPLICATION:
			return transformApplication((BlipApplication) arguments.get(0));
		case BlipLanguagePackage.ANDROID_TRANSFORMER___TRANSFORM_MODEL_TO_ENTITY__BLIPMODEL_FOLDER:
			return transformModelToEntity((BlipModel) arguments.get(0),
					(Folder) arguments.get(1));
		}
		return super.eInvoke(operationID, arguments);
	}

} //AndroidTransformerImpl
